package main

import (
	"fmt"
	"math"
	"os"
)

func sum(vals ...int) int {
	total := 0
	for _, val := range vals {
		total += val
	}
	return total
}

func errorf(linenum int, format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, "Line %d:", linenum)
	fmt.Fprintf(os.Stderr, format, args...)
	fmt.Fprintln(os.Stderr)
}

// 练习5.15: 编写类似sum的可变参数函数max和min。考虑不传参时，max和min该如何处理，再编写至少接收1个参数的版本。
func max(vals ...float64) float64 {
	max := math.Inf(-1)
	for _, val := range vals {
		if max < val {
			max = val
		}
	}
	return max
}

func min(vals ...float64) float64 {
	min := math.Inf(1)
	for _, val := range vals {
		if min > val {
			min = val
		}
	}
	return min
}

func main() {
	fmt.Println(sum())
	fmt.Println(sum(3))
	fmt.Println(sum(1, 2, 3, 4))
	// linenum, name := 12, "count"
	// errorf(linenum, "undefined: %s", name)
	fmt.Println(max())
	fmt.Println(max(3))
	fmt.Println(max(1, 2, 3, 4))
	fmt.Println(min())
	fmt.Println(min(3))
	fmt.Println(min(1, 2, 3, 4))

}
